package com.ly.base.chapter03;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Collector 收集器
 */
public class ALCollectorDemo {
    record Hero(String name, int strength) { }

    public static void main(String[] args) {
        Stream<String> stream = Stream.of("令狐冲", "风清扬", "独孤求败", "方证",
                "东方不败", "冲虚", "向问天", "任我行", "不戒");
        /**
         * 1)List集合
         * ArrayList::new    () -> new ArrayList()
         * ArrayList::add    (list,x) -> list.add(x)
         */
//        List<String> result = stream.collect(Collectors.toList());
//        List<String> result = stream.toList();


        // 2)Set集合（重复数据不会重复添加）
//        Set<String> result = stream.collect(Collectors.toSet());

        // 3)Map集合
//        Map<String, Integer> result = stream.collect(Collectors.toMap(x -> x, x -> 1));

        // 4)字符串容器
//        String result = stream.collect(Collectors.joining());

        // 5)字符串容器拼接加 分隔符
//        String result = stream.collect(Collectors.joining(","));

        // 6)分组收集
        /**
         *groupingBy(组名，下游收集器)
         */
        //{2=[方证, 冲虚, 不戒], 3=[令狐冲, 风清扬, 向问天, 任我行], 4=[独孤求败, 东方不败]}
//        Map<Integer, List<String>> result = stream.collect(Collectors.groupingBy(String::length, Collectors.toList()));
        //{2=方证,冲虚,不戒, 3=令狐冲,风清扬,向问天,任我行, 4=独孤求败,东方不败}
        Map<Integer, String> result = stream.collect(Collectors.groupingBy(String::length, Collectors.joining(",")));

        System.out.println(result);
    }
}
